package com.szcatic.ework.listener;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;

import com.szcatic.ework.service.login.LoginService;
import com.szcatic.ework.vo.other.LoginInfo;

public class ShiroSessionListener extends SessionListenerAdapter{
	
	@Autowired
	private LoginService loginService;

	@Override
	public void onStart(Session session) {
		System.out.println("会话创建:" + session.getId().toString());
		super.onStart(session);
	}

	@Override
	public void onStop(Session session) {
		System.out.println("会话停止:" + session.getId().toString());
		LoginInfo loginInfo = new LoginInfo();
		loginInfo.setSessionId(session.getId().toString());
		loginService.updateLoginInfo(loginInfo);
		super.onStop(session);
	}

	@Override
	public void onExpiration(Session session) {
		System.out.println("会话过期:" + session.getId().toString());
		if (session.getAttribute("user") == null) {
			super.onExpiration(session);
		} else {
		LoginInfo loginInfo = new LoginInfo();
		loginInfo.setSessionId(session.getId().toString());
		loginService.updateLoginInfo(loginInfo);
		super.onExpiration(session);
		}
		
	}

}
